package com.superm.controller;

import com.alibaba.fastjson.JSON;
import com.superm.entity.RelationShip;
import com.superm.entity.RelationShipPoJo;
import com.superm.entity.RelationShipType;
import com.superm.service.RelationShipService;
import com.superm.service.RelationShipTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/relationShip")
public class RelationShipController {

    @Autowired
    private RelationShipTypeService relationShipTypeService;

    @Autowired
    private RelationShipService relationShipService;
    /**
     * 跳转到商品分类页面
     * @return
     */
    @RequestMapping("/skipRelationShip")
    public ModelAndView skipRelationShip(){
        ModelAndView mv = new ModelAndView("relationship/relationshipList");
        //查询所有的类型信息
        List<RelationShipType> listRelationType =relationShipTypeService.selectRealtionShipTypeList();
        mv.addObject("listRelationType",listRelationType);
        return mv;
    }


    /**
     * 跳转到修改商品分类
     * @return
     */

    @RequestMapping("/skipRelationShipUpdate")
    public ModelAndView skipRelationShipUpdate(@RequestParam("relationshipId")Integer relationshipId){
        ModelAndView mv = new ModelAndView("relationship/relationshipUpdate");
        //根据编号查询指定类型
        RelationShip relationShip = relationShipService.selectIdRelationShip(relationshipId);
        //查询全部的大类型
        List<RelationShip> relationShipList = relationShipService.selectPidMaxRelationShip();
        //查询所有类型
        List<RelationShipType> listRelationType =relationShipTypeService.selectRealtionShipTypeList();

        //放入作用域
        mv.addObject("relationShip",relationShip);
        mv.addObject("relationShipList",relationShipList);
        mv.addObject("listRelationType",listRelationType);
        return  mv;
    }

    /**
     * 跳转到修改商品分类
     * @return
     */

    @RequestMapping("/skipRelationShipUpdateOne")
    public ModelAndView skipRelationShipUpdateOne(@RequestParam("relationshipId")Integer relationshipId){
        ModelAndView mv = new ModelAndView("relationship/relationShipUpdateOne");
        //根据编号查询指定类型
        RelationShip relationShip = relationShipService.selectIdRelationShip(relationshipId);
        //放入作用域
        mv.addObject("relationShip",relationShip);
        return  mv;
    }




    /**
     * 查询所有底下的子
     * @return
     */
    @RequestMapping("selectPidAll")
    @ResponseBody
    public Map<String,Object> selectPidAll(@RequestParam("relationshipId")Integer relationshipId){
        Map<String,Object> map = new HashMap<String,Object>();
        //根据编号查询指定类型
        List<RelationShip> relationShipList = relationShipService.selectPidList(relationshipId);
        map.put("data", JSON.toJSONString(relationShipList));
        return map;
    }


    /**
     * 模糊查询分类信息
     * @param relationShipPoJo
     * @return
     */
    @RequestMapping("/selectListRelationShip")
    @ResponseBody
    public Map<String,Object> selectListRelationShip(RelationShipPoJo relationShipPoJo, @RequestParam("page") Integer pageNo,@RequestParam("limit")Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        System.out.println(relationShipPoJo);
        //查询分类信息
        List<RelationShip> relationShipList = relationShipService.selectRelationShipLike(relationShipPoJo,pageNo,pageSize);
        //查询总条数
        int count = relationShipService.selectRelationShipCount(relationShipPoJo);
        //响应数据
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",relationShipList);
        return map;
    }

    /**
     * 修改商品分类信息
     * @param relationShipPoJo
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateRelationShip")
    public Map<String,Object> updateRelationShip(RelationShipPoJo relationShipPoJo){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);

        if (relationShipService.updateRelationShip(relationShipPoJo)>0){
            map.put("success",true);
            map.put("msg","修改成功");
        }else{
            map.put("success",false);
            map.put("msg","修改失败");
        }
        return map;
    }

    /**
     * 修改商品分类信息
     * @param relationShipPoJo
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateRelationShipOne")
    public Map<String,Object> updateRelationShipOne(RelationShipPoJo relationShipPoJo){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);

        if (relationShipService.updateRelationShipOne(relationShipPoJo)>0){
            map.put("success",true);
            map.put("msg","修改成功");
        }else{
            map.put("success",false);
            map.put("msg","修改失败");
        }
        return map;
    }



    /**
     * 跳转到添加一级分类页面
     * @param
     * @return
     */
    @RequestMapping("/relationShipAddOne")
    public ModelAndView relationShipAddOne(){
        ModelAndView mv = new ModelAndView("relationship/relationshipAddOne");
        return mv;
    }

    @ResponseBody
    @RequestMapping("/addOneRelationShip")
    public Map<String,Object> addOneRelationShip(@RequestParam("relationshipName") String relationShipName){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);

        if (relationShipService.addRelationShipOne(relationShipName)>0){
            map.put("success",true);
            map.put("msg","添加成功");
        }else{
            map.put("success",false);
            map.put("msg","添加失败");
        }
        return map;
    }

    /**
     * 跳转添加二级分类
     * @return
     */
    @RequestMapping("/skipAddRelationShipTwo")
    public ModelAndView skipAddRelationShipTwo(){
        ModelAndView mv = new ModelAndView("relationship/relationShipAddTow");
        //查询全部的大类型
        List<RelationShip> relationShipList = relationShipService.selectPidMaxRelationShip();
        mv.addObject("relationShipList",relationShipList);
        return mv;
    }


    /**
     * 添加二级分类
     * @param relationShipPoJo
     * @return
     */
    @RequestMapping("/addRelationShipTwo")
    @ResponseBody
    public Map<String,Object> addRelationShipTwo(RelationShipPoJo relationShipPoJo){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        if (relationShipService.addRelationShipTow(relationShipPoJo)>0){
            map.put("success",true);
            map.put("msg","添加成功");
        }else{
            map.put("success",false);
            map.put("msg","添加失败");
        }
        return map;
    }

    /**
     * 删除商品分类信息
     * @return
     */
    @RequestMapping("/deleteRelationShip")
    @ResponseBody
    public Map<String,Object> deleteRelationShip(@RequestParam("relationshipId") Integer relationshipId){
        //执行删除
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        if (relationShipService.deleteRelationShip(relationshipId)>0){
            map.put("success",true);
            map.put("msg","删除成功");
        }else{
            map.put("success",false);
            map.put("msg","该类型下有子类型,无法删除!");
        }
        return map;
    }
}
